package com.loong.common.mybatis.Interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Properties;

//@Component
@Slf4j
@Intercepts(@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}))
public class CommonObjectInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws IllegalAccessException, InvocationTargetException {
        fillField(invocation);
        return invocation.proceed();
    }


    private void fillField(Invocation invocation) {
        Object[] args = invocation.getArgs();
        SqlCommandType sqlCommandType = null;
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            String className = arg.getClass().getName();
            log.info(i + " 参数类型：" + className);
            //第一个参数处理。根据它判断是否给“操作属性”赋值。
            if (arg instanceof MappedStatement) {//如果是第一个参数 MappedStatement
                MappedStatement ms = (MappedStatement) arg;
                sqlCommandType = ms.getSqlCommandType();
                log.info("操作类型：" + sqlCommandType);
                if (sqlCommandType == SqlCommandType.INSERT || sqlCommandType == SqlCommandType.UPDATE) {//如果是“增加”或“更新”操作，则继续进行默认操作信息赋值。否则，则退出
                    continue;
                } else {
                    break;
                }
            }

            if (sqlCommandType == SqlCommandType.INSERT) {
                for (Field f : arg.getClass().getDeclaredFields()
                ) {
                    f.setAccessible(true);
                    switch (f.getName()) {
                        case "createBy":
                            setProperty(arg, f, "111");
                            break;
                        case "createTime":
                            setProperty(arg, f, new Date());
                            break;
                        case "updateBy":
                            setProperty(arg, f, "111");
                            break;
                        case "updateTime":
                            setProperty(arg, f, new Date());
                            break;
                        case "delFlag":
                            setProperty(arg, f, "0");
                            break;
                    }
                }
            } else if (sqlCommandType == SqlCommandType.UPDATE) {
                for (Field f : arg.getClass().getDeclaredFields()
                ) {
                    f.setAccessible(true);
                    switch (f.getName()) {
                        case "updateBy":
                            setProperty(arg, f, "111");
                            break;
                        case "updateTime":
                            setProperty(arg, f, new Date());
                            break;
                    }
                }
            }
        }
    }

    /**
     * 为对象的操作属性赋值
     *
     * @param bean
     */
    private void setProperty(Object bean, Field field, Object value) {
        //根据需要，将相关属性赋上默认值
        field.setAccessible(true);
        ReflectionUtils.setField(field, bean, value);
    }

    @Override
    public Object plugin(Object o) {

        return Plugin.wrap(o, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
